package com.alan.wifishare.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alan.wifishare.R;
import com.alan.wifishare.view.ui.CircleProgressDialog;

/**
 * @className: BaseActivity
 * @describtion: BaseActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 12:51
 */
public abstract class BaseActivity extends Activity {

    private LayoutInflater layoutInflater;
    private LinearLayout parentLinearLayout;
    private CircleProgressDialog circleProgressDialog;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContentView(R.layout.base_layout);
    }

    protected abstract void initView();

    private void initContentView(int layoutResID) {
        layoutInflater = LayoutInflater.from(this);
        ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
        viewGroup.removeAllViews();
        View view = layoutInflater.inflate(layoutResID, null);
        parentLinearLayout = (LinearLayout) view.findViewById(R.id.baseLayout);
        viewGroup.addView(parentLinearLayout);
    }

    @Override
    public void setContentView(View view) {
        parentLinearLayout.addView(view);
    }

    @Override
    public void setContentView(int layoutResID) {
        layoutInflater.inflate(layoutResID, parentLinearLayout, true);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        parentLinearLayout.addView(view, params);
    }

    public void toastMsgShort(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void toastMsgLong(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    protected void progressDialogShow(){
        if(circleProgressDialog == null){
            circleProgressDialog = new CircleProgressDialog(this, R.style.Progress_Dialog);
        }
        circleProgressDialog.show();
    }

    protected void progressDialogHiden(){
        if(circleProgressDialog != null && circleProgressDialog.isShowing()){
            circleProgressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        progressDialogHiden();
    }
}
